import { CMEVENTS } from '@amcax/base';
import { wsApi } from '../server/wsApi';
import { userEvents } from '../module';

export class CmdManager {
  private static instance: CmdManager;
  private _undo: number = 0;
  private _redo: number = 0;
  private _airfoilCreated: boolean = false;

  private constructor() {}
  public static getInstance(): CmdManager {
    if (!CmdManager.instance) {
      CmdManager.instance = new CmdManager();
    }
    return CmdManager.instance;
  }

  async updateUndoSize() {
    const { undo, redo } = (await wsApi.getUndoSize()) as any;
    console.log('updateUndoSize', undo, redo);
    this._undo = undo;
    this._redo = redo;
    userEvents.emit(CMEVENTS.CMD_MGR_EXECUTED, this);
  }

  increaseUndo(): void {
    this._undo++;
    userEvents.emit(CMEVENTS.CMD_MGR_EXECUTED, this);
  }

  get canUndo(): boolean {
    return this._undo > 0;
  }

  get canRedo(): boolean {
    return this._redo > 0;
  }

  async undo() {
    if (!this.canUndo) {
      console.warn('no more undo command');
      return;
    }
    console.log('execute undo !!!!');
    await wsApi.undo();
    this.updateUndoSize();
  }

  async redo() {
    if (!this.canRedo) {
      console.warn('no more redo command');
      return;
    }
    console.log('execute redo !!!!');
    await wsApi.redo();
    this.updateUndoSize();
  }

  // =============== 翼型相关 ================
}

export const cmdMgr = CmdManager.getInstance();
